package com.kkb.proxy.dynamic;

import com.kkb.proxy.statix.AOP;
import com.kkb.proxy.statix.LogAop;
import com.kkb.proxy.statix.TranAOP;
import com.kkb.service.IService;
import com.kkb.service.NoInterfaceService;
import com.kkb.service.OrderService;

/**
 * 动态代理
 *
 * @author bing.zhang06@hand-china.com 2021/8/28 14:43
 **/
public class Proxy {
    public static void jdkProxy() {
        //目标对象--被代理对象
        OrderService orderService = new OrderService();

        //切面
        AOP tranAop = new TranAOP();
        AOP logAop = new LogAop();

        //代理对象1: 事务
        IService $proxy1 = (IService) new JdkProxyFactory(orderService, tranAop).getProxyInstance();
        $proxy1.selectCount();

        System.out.println();

        //代理对象2: 日志
        IService $proxy2 = (IService) new JdkProxyFactory($proxy1, logAop).getProxyInstance();
        $proxy2.selectCount();
    }

    public static void cglibProxy() {
        //目标对象--被代理对象
        NoInterfaceService service = new NoInterfaceService();

        //切面
        AOP tranAop = new TranAOP();
        AOP logAop = new LogAop();

        //代理对象1: 事务
        NoInterfaceService $proxy3
                = (NoInterfaceService) new CglibProxyFactory(service, tranAop).getProxyInstance();
        $proxy3.sayHello();

        System.out.println();

        //代理对象2: 日志
        NoInterfaceService $proxy4
                = (NoInterfaceService) new CglibProxyFactory(service, logAop).getProxyInstance();
        $proxy4.sayHello();

    }
}
